$(function () {
    getuserinfo()
    var layer = layui.layer
    $('#btnLogout').on('click', function () {
        layer.confirm('确定退出登录?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index)
        })
    })
})
function getuserinfo() {
    $.ajax({
        method: 'GET',
        url: '/my/userinfo',
        success: function (res) {
            if (res.status !== 0) {
                return layui.layer.msg('获取用户信息失败！')
            }
            renderAvatar(res.data)
        }
    })
}
function renderAvatar(data) {
    let name = data.nickname || data.username
    $("#welcome").html('欢迎' + name)
    let first = name[0].toUpperCase()
    if (data.user_pic == null) {
        $(".layui-nav-img").hide()
        $(".text-avatar").html(first)
    } else {
        $(".text-avatar").hide()
        $(".layui-nav-img").attr('src', data.user_pic)
    }
}